//
//  CZNewFeatureViewController.swift
//  01-sinaWeibo-05-09
//
//  Created by mac on 16/5/16.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

//显示新特性界面
private let reuseIdentifier = "Cell"
private let ItemCoumt = 4

class CZNewFeatureViewController: UICollectionViewController {

    private let layout = UICollectionViewFlowLayout()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    init () {
        super.init(collectionViewLayout: layout)
    }

  
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
     prepareCollectionView()

    }
    private func prepareCollectionView(){
        //注册cell

       self.collectionView?.registerClass(CZNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)


        //设置layout参数
        //让cell和屏幕一样大
        layout.itemSize = UIScreen.mainScreen().bounds.size
        //水平滚动
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        //间距
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        
        //取消弹簧效果
        collectionView?.bounces = false
        //取消滚动条
        collectionView?.showsHorizontalScrollIndicator = false
        //分页效果
        collectionView?.pagingEnabled = true
        
    }
    


}
//扩展
extension CZNewFeatureViewController{



    
    //某一组里面有几个cell
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return ItemCoumt
    }
    
    //每一个cell长什么样子
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        indexPath.section
        indexPath.row
        
       let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CZNewFeatureCell
        
        cell.backgroundColor = UIColor.brownColor()
        cell.index = indexPath.item
        
        return cell
    }
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        //先获得collectionView的偏移值,计算出当前停下来的是那一页
        let index = Int(scrollView.contentOffset.x / UIScreen.mainScreen().bounds.width)
        CZPrint(items: "停止在第\(index)页")
        
        //判断是否是最后一页
        if index == ItemCoumt - 1{
            //获取cell,千万不要自己去创建cell ,应该拿到正在显示的cell
            let indexPath = NSIndexPath(forItem: index, inSection: 0)
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! CZNewFeatureCell
            
            cell.startAnimation()
            
        }
    }

}







